package org.haifi.game.mina;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

/**
 * 解码器
 * 
 * 头两个字节是protobuf对象的长度
 * 
 * 两个字节的指令id
 * 
 * 一个字节的loop,用来进行验证
 * 
 * 然后是protobuf对象数据
 * 
 * @author joycube2
 * 
 */
public class GameDecoderFilter extends ProtocolDecoderAdapter {

	@Override
	public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out)
			throws Exception {
		short protoDataLen = in.getShort();
		short cmdId = in.getShort();
		byte loop = in.get();
		byte[] protoData = new byte[protoDataLen];
		in.get(protoData);
		GameCoderObject object = new GameCoderObject();
		object.cmdId = cmdId;
		object.loop = loop;
		object.protoData = protoData;
		out.write(object);
	}

}
